{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7ecc7207",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T06:57:11.389929Z",
     "start_time": "2021-11-01T06:57:06.484642Z"
    }
   },
   "outputs": [],
   "source": [
    "from autogluon.tabular import TabularDataset, TabularPredictor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4a392ef1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T06:57:21.699399Z",
     "start_time": "2021-11-01T06:57:21.667644Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['sample_submission.csv',\n",
       " 'train.csv',\n",
       " 'new_merchant_transactions.csv',\n",
       " 'historical_transactions.csv',\n",
       " 'test.csv',\n",
       " 'merchants.csv']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.listdir('../data/elo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6bc8546b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T06:57:30.656950Z",
     "start_time": "2021-11-01T06:57:30.081312Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "train_data = TabularDataset('../data/elo/train.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "21370fd8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T06:57:31.686297Z",
     "start_time": "2021-11-01T06:57:31.680042Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(201917, 6)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ec3eacc4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T06:57:41.089411Z",
     "start_time": "2021-11-01T06:57:41.085849Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "label = 'target'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a225cdb6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T07:35:54.398712Z",
     "start_time": "2021-11-01T06:57:45.977677Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning: Training may take a very long time because `time_limit` was not specified and `train_data` is large (201917 samples, 33.92 MB).\n",
      "\tConsider setting `time_limit` to ensure training finishes within an expected duration or experiment with a small portion of `train_data` to identify an ideal `presets` and `hyperparameters` configuration.\n",
      "Beginning AutoGluon training ...\n",
      "AutoGluon will save models to \"agModels-kaggle_elo/\"\n",
      "AutoGluon Version:  0.2.1b20210716\n",
      "Train Data Rows:    201917\n",
      "Train Data Columns: 5\n",
      "Preprocessing data ...\n",
      "AutoGluon infers your prediction problem is: 'regression' (because dtype of label-column == float and many unique label-values observed).\n",
      "\tLabel info (max, min, mean, stddev): (17.9650684, -33.21928095, -0.39364, 3.8505)\n",
      "\tIf 'regression' is not the correct problem_type, please manually specify the problem_type argument in fit() (You may specify problem_type as one of: ['binary', 'multiclass', 'regression'])\n",
      "Using Feature Generators to preprocess the data ...\n",
      "Fitting AutoMLPipelineFeatureGenerator...\n",
      "\tAvailable Memory:                    165045.47 MB\n",
      "\tTrain Data (Original)  Memory Usage: 32.31 MB (0.0% of available memory)\n",
      "\tInferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.\n",
      "\tStage 1 Generators:\n",
      "\t\tFitting AsTypeFeatureGenerator...\n",
      "\tStage 2 Generators:\n",
      "\t\tFitting FillNaFeatureGenerator...\n",
      "\tStage 3 Generators:\n",
      "\t\tFitting IdentityFeatureGenerator...\n",
      "\t\tFitting CategoryFeatureGenerator...\n",
      "\t\t\tFitting CategoryMemoryMinimizeFeatureGenerator...\n",
      "\t\tFitting DatetimeFeatureGenerator...\n",
      "\tStage 4 Generators:\n",
      "\t\tFitting DropUniqueFeatureGenerator...\n",
      "\tUnused Original Features (Count: 1): ['card_id']\n",
      "\t\tThese features were not used to generate any of the output features. Add a feature generator compatible with these features to utilize them.\n",
      "\t\tFeatures can also be unused if they carry very little information, such as being categorical but having almost entirely unique values or being duplicates of other features.\n",
      "\t\tThese features do not need to be present at inference time.\n",
      "\t\t('object', []) : 1 | ['card_id']\n",
      "\tTypes of features in original data (raw dtype, special dtypes):\n",
      "\t\t('int', [])                        : 3 | ['feature_1', 'feature_2', 'feature_3']\n",
      "\t\t('object', ['datetime_as_object']) : 1 | ['first_active_month']\n",
      "\tTypes of features in processed data (raw dtype, special dtypes):\n",
      "\t\t('int', [])                  : 3 | ['feature_1', 'feature_2', 'feature_3']\n",
      "\t\t('int', ['datetime_as_int']) : 1 | ['first_active_month']\n",
      "\t3.6s = Fit runtime\n",
      "\t4 features in original data used to generate 4 features in processed data.\n",
      "\tTrain Data (Processed) Memory Usage: 6.46 MB (0.0% of available memory)\n",
      "Data preprocessing and feature engineering runtime = 3.81s ...\n",
      "AutoGluon will gauge predictive performance using evaluation metric: 'root_mean_squared_error'\n",
      "\tTo change this, specify the eval_metric argument of fit()\n",
      "Automatically generating train/validation split with holdout_frac=0.012381324999876186, Train Rows: 199417, Val Rows: 2500\n",
      "Fitting 11 L1 models ...\n",
      "Fitting model: KNeighborsUnif ...\n",
      "\t-3.8676\t = Validation root_mean_squared_error score\n",
      "\t38.93s\t = Training runtime\n",
      "\t1.09s\t = Validation runtime\n",
      "Fitting model: KNeighborsDist ...\n",
      "\t-3.8676\t = Validation root_mean_squared_error score\n",
      "\t36.91s\t = Training runtime\n",
      "\t1.53s\t = Validation runtime\n",
      "Fitting model: LightGBMXT ...\n",
      "\t-3.4119\t = Validation root_mean_squared_error score\n",
      "\t6.98s\t = Training runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: LightGBM ...\n",
      "\t-3.4109\t = Validation root_mean_squared_error score\n",
      "\t4.97s\t = Training runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: RandomForestMSE ...\n",
      "\t-3.4282\t = Validation root_mean_squared_error score\n",
      "\t4.53s\t = Training runtime\n",
      "\t0.74s\t = Validation runtime\n",
      "Fitting model: CatBoost ...\n",
      "\t-3.4109\t = Validation root_mean_squared_error score\n",
      "\t1.23s\t = Training runtime\n",
      "\t0.03s\t = Validation runtime\n",
      "Fitting model: ExtraTreesMSE ...\n",
      "\t-3.4287\t = Validation root_mean_squared_error score\n",
      "\t3.84s\t = Training runtime\n",
      "\t0.66s\t = Validation runtime\n",
      "Fitting model: NeuralNetFastAI ...\n",
      "/home/caihengxing/anaconda3/envs/python37/lib/python3.7/site-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 10010). Please update your GPU driver by downloading and installing a new version from the URL: http://www.nvidia.com/Download/index.aspx Alternatively, go to: https://pytorch.org to install a PyTorch version that has been compiled with your version of the CUDA driver. (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:115.)\n",
      "  return torch._C._cuda_getDeviceCount() > 0\n",
      "\t-3.4103\t = Validation root_mean_squared_error score\n",
      "\t2167.15s\t = Training runtime\n",
      "\t0.46s\t = Validation runtime\n",
      "Fitting model: XGBoost ...\n",
      "\t-3.4119\t = Validation root_mean_squared_error score\n",
      "\t6.24s\t = Training runtime\n",
      "\t0.04s\t = Validation runtime\n",
      "Fitting model: NeuralNetMXNet ...\n",
      "\tWarning: Exception caused NeuralNetMXNet to fail during training... Skipping this model.\n",
      "\t\tLegacy mxnet==1.4.0 detected, some new modules will not work properly. mxnet>=1.6.0 is required. You can use pip to upgrade mxnet `pip install mxnet --upgrade` or `pip install mxnet_cu101 --upgrade`\n",
      "Detailed Traceback:\n",
      "Traceback (most recent call last):\n",
      "  File \"/mnt/disk0/home/caihengxing/software/autogluon-master/tabular/src/autogluon/tabular/trainer/abstract_trainer.py\", line 960, in _train_and_save\n",
      "    model = self._train_single(X, y, model, X_val, y_val, **model_fit_kwargs)\n",
      "  File \"/mnt/disk0/home/caihengxing/software/autogluon-master/tabular/src/autogluon/tabular/trainer/abstract_trainer.py\", line 932, in _train_single\n",
      "    model = model.fit(X=X, y=y, X_val=X_val, y_val=y_val, **model_fit_kwargs)\n",
      "  File \"/mnt/disk0/home/caihengxing/software/autogluon-master/core/src/autogluon/core/models/abstract/abstract_model.py\", line 518, in fit\n",
      "    out = self._fit(**kwargs)\n",
      "  File \"/mnt/disk0/home/caihengxing/software/autogluon-master/tabular/src/autogluon/tabular/models/tabular_nn/tabular_nn_model.py\", line 168, in _fit\n",
      "    try_import_mxnet()\n",
      "  File \"/mnt/disk0/home/caihengxing/software/autogluon-master/core/src/autogluon/core/utils/try_import.py\", line 40, in try_import_mxnet\n",
      "    raise ValueError(msg)\n",
      "ValueError: Legacy mxnet==1.4.0 detected, some new modules will not work properly. mxnet>=1.6.0 is required. You can use pip to upgrade mxnet `pip install mxnet --upgrade` or `pip install mxnet_cu101 --upgrade`\n",
      "Fitting model: LightGBMLarge ...\n",
      "\t-3.4125\t = Validation root_mean_squared_error score\n",
      "\t0.64s\t = Training runtime\n",
      "\t0.0s\t = Validation runtime\n",
      "Fitting model: WeightedEnsemble_L2 ...\n",
      "\t-3.4098\t = Validation root_mean_squared_error score\n",
      "\t1.26s\t = Training runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "AutoGluon training complete, total runtime = 2288.32s ...\n",
      "TabularPredictor saved. To load, use: predictor = TabularPredictor.load(\"agModels-kaggle_elo/\")\n"
     ]
    }
   ],
   "source": [
    "save_path = 'agModels-kaggle_elo'  # specifies folder to store trained models\n",
    "predictor = TabularPredictor(label=label, path=save_path).fit(train_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fa84c4eb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T07:35:54.783031Z",
     "start_time": "2021-11-01T07:35:54.402853Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/caihengxing/anaconda3/envs/python37/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n",
      "  and should_run_async(code)\n",
      "Loaded data from: ../data/elo/test.csv | Columns = 5 / 5 | Rows = 123623 -> 123623\n"
     ]
    }
   ],
   "source": [
    "test_data_nolab = TabularDataset('../data/elo/test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "54cce4ce",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T07:36:12.432054Z",
     "start_time": "2021-11-01T07:35:54.787479Z"
    }
   },
   "outputs": [],
   "source": [
    "y_pred = predictor.predict(test_data_nolab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ca6cc9bf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T07:36:12.460875Z",
     "start_time": "2021-11-01T07:36:12.437689Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        -0.461080\n",
       "1        -0.615424\n",
       "2        -0.200246\n",
       "3        -0.072107\n",
       "4        -0.592423\n",
       "            ...   \n",
       "123618   -0.086364\n",
       "123619   -0.190979\n",
       "123620   -0.580392\n",
       "123621   -0.359016\n",
       "123622   -0.538284\n",
       "Name: target, Length: 123623, dtype: float32"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "240cd16c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T07:36:12.470132Z",
     "start_time": "2021-11-01T07:36:12.464014Z"
    }
   },
   "outputs": [],
   "source": [
    "id_ = ['card_id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b4fd354e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T07:36:12.604225Z",
     "start_time": "2021-11-01T07:36:12.473130Z"
    }
   },
   "outputs": [],
   "source": [
    "sub = test_data_nolab[id_].copy()\n",
    "sub[label] = list(y_pred.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c59b336a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-01T07:36:13.307716Z",
     "start_time": "2021-11-01T07:36:12.607615Z"
    }
   },
   "outputs": [],
   "source": [
    "sub.to_csv(\"./autogluon_sub_kaggle_elo.csv\", index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c43926aa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b54628e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
